package com.lanou3g.ptp.task.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.lanou3g.ptp.base.service.BaseService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.message.service.MessageService;
import com.lanou3g.ptp.task.mapper.TempRecordMapper;
import com.lanou3g.ptp.task.mapper.TempSignInMapper;
import com.lanou3g.ptp.task.service.TaskService;
import com.lanou3g.ptp.task.service.TempSignService;
import com.lanou3g.ptp.taskstudent.service.TaskStudentService;
import com.lanou3g.ptp.utils.DateUtils;
import com.lanou3g.ptp.vo.RecordVO;
import com.lanou3g.ptp.vo.TemplateData;
import com.lanou3g.ptp.vo.WxTmplMsg;
import com.lanou3g.ptp.wx.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = TempSignService.class, timeout = 5000)
@Slf4j
public class TempSignServiceImpl implements TempSignService {

    @Resource
    private TempRecordMapper tempRecordMapper;

    @Resource
    private TempSignInMapper tempSignInMapper;

    @Autowired
    private TaskStudentService taskStudentService;

    @Autowired
    private TaskService taskService;

    @Reference
    private BaseService baseService;

    @Reference
    private WxService wxService;

    @Reference
    private MessageService messageService;


    @Override
    public boolean addRecord(RecordVO record) {
        Date now = DateUtils.now();
        Long duration = record.getDuration();
        Date endTime = new Date(now.getTime() + duration * 60 * 1000);
        TempRecord tr = TempRecord.builder()
                .taskId(record.getTaskId())
                .startTime(now)
                .endTime(endTime)
                .status(0)
                .createTime(now)
                .updateTime(now)
                .build();
        int i = this.tempRecordMapper.insertOne(tr);
        if(i<0){
            return false;
        }else {
            notifyStudents(tr);
            return true;
        }
    }

    private void notifyStudents(TempRecord tempRecord) {
        Long taskId = tempRecord.getTaskId();
        List<Student> students = taskStudentService.findStudentsByTaskId(taskId);
        Task task = taskService.findById(taskId);
        Base base = baseService.findById(task.getBaseId());
        for (Student student : students) {
            String openId = student.getOpenId();
            if (openId != null && !openId.trim().equals("")) {
                //需要推送给小程序的数据
                Map<String, TemplateData> data = new HashMap<>();
                data.put("keyword1", new TemplateData("地址围栏"));
                data.put("keyword2", new TemplateData(task.getName()));
                data.put("keyword3", new TemplateData(base.getAddress()));
                long duration = (tempRecord.getEndTime().getTime() - tempRecord.getStartTime().getTime()) / (1000 * 60);
                String content = "临时查岗：请在 " + duration + " 分钟内完成签到！";
                data.put("keyword4", new TemplateData(content));
                boolean result = wxService.sendMessage(openId, WxTmplMsg.TYPE_TEMP_SIGN, data);
                if (result) {
                    Message message = new Message();
                    message.setStudentId(student.getId());
                    message.setTaskId(taskId);
                    message.setContent(content);
                    messageService.saveTempSignMessage(message);
                }
            }
        }
    }

    @Override
    public List<TempRecord> findRecordsByTaskId(Long taskId) {
        return this.tempRecordMapper.findByTaskId(taskId);
    }

    @Override
    public List<TempSignIn> tempSignInDetails(Long recordId) {
        // 当前任务下的所有学生的临时打卡情况
        TempRecord record = this.tempRecordMapper.findById(recordId);
        return this.tempSignInMapper.findSignInDetails(recordId, record.getTaskId());
    }
}
